package tut.concurrentPackage.src.CopyOnWriteArrayList;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class IteratorRemoveTest {
	public final static void main(String args[]) {
		ArrayList<String> list = new ArrayList<String>();
		list.add("vivek");
		list.add("kumar");
		Iterator<String> i = list.iterator();
		while (i.hasNext()) {
			System.out.println(i.next());
			i.remove();  // we can use iterator’s method (remove) to modify the list. Now check for CopyOnWriteArrayList
		}
		System.out.println("After modification:");
		Iterator<String> i2 = list.iterator();
		while (i2.hasNext()) {
			System.out.println(i2.next());
		}

		System.out.println("-----------------------");
		CopyOnWriteArrayList<String> list3 = new CopyOnWriteArrayList<String>();
		list3.add("vivek");
		list3.add("kumar");
		Iterator<String> i3 = list3.iterator();
		while (i3.hasNext()) {
			System.out.println(i3.next());
			list3.add("abhishek");
			i3.remove();  // we cannot use iterator method to modify the elements of CopyOnWriteArrayList
		}

	}
}
